#include "BaseScene.h"
#include "EarthWrapper.h"
#include "SceneContext.h"

osg::Group* SceneContext::getMapSceneNode()
{
	return EarthWrapper::instance()->getSceneNode();
}

osg::Group* SceneContext::getUnMapSceneNode()
{
	return EarthWrapper::instance()->getUnMapSceneNode();
}

osg::Group* SceneContext::getRootNode()
{
	return EarthWrapper::instance()->getRootNode();
}

osg::Camera* SceneContext::getCamera()
{
	return EarthWrapper::instance()->getCamera();
}


osgEarth::Util::EarthManipulator* SceneContext::getEarthManipulator()
{
	return EarthWrapper::instance()->getManipulator();
}

osgViewer::Viewer* SceneContext::getViewer()
{
	return EarthWrapper::instance()->getViewer();
}

osgEarth::MapNode* SceneContext::getMapNode()
{
	return EarthWrapper::instance()->getMapNode();
}

const osgEarth::SpatialReference* SceneContext::getSpatialReference()
{
	return EarthWrapper::instance()->getMapNode()->getMapSRS()->getGeodeticSRS();
}

const osg::EllipsoidModel* SceneContext::getEllipsoidModel()
{
	return EarthWrapper::instance()->getEllipsoidModel();
}



BaseScene::BaseScene()
{
}


BaseScene::~BaseScene()
{
}

osg::Node* BaseScene::createNode()
{
	return 0;
}

osg::Node* BaseScene::getOrCreateNode()
{
	if (!this->_node){
		this->_node = createNode();
	}
	return _node;
}

osg::Node* BaseScene::getNode()
{
	return _node;
}

void BaseScene::open()
{
	if (_node) return;
	_node = this->createNode();
	if (_node){
		this->getSceneContext()->getRootNode()->addChild(_node);
	}
}

void BaseScene::close()
{
	if (_node){
		this->getSceneContext()->getRootNode()->removeChild(_node);
		_node = 0;
	}
}

void BaseScene::setVisible(bool b)
{
	if (_node) {
		_node->setNodeMask(b ? 1 : 0);
	}
}

SceneContext* BaseScene::getSceneContext()
{
	static SceneContext sc;
	return &sc;
}

EarthScene::EarthScene()
{

}

EarthScene::~EarthScene()
{

}

void EarthScene::open()
{
	if (_node) return;
	_node = this->createNode();
	if (_node) {
		this->getSceneContext()->getMapSceneNode()->addChild(_node);
	}
}

void EarthScene::close()
{
	if (_node) {
		this->getSceneContext()->getMapSceneNode()->removeChild(_node);
		_node = 0;
	}
}